//
//  DataCenter.m
//  51-Tour
//
//  Created by xiang ying on 14-3-21.
//  Copyright (c) 2014年 xiang ying. All rights reserved.
//

#import "DataCenter.h"

static DataCenter *data = nil;


@interface DataCenter(){
    NSMutableDictionary *language;
}

@end


@implementation DataCenter
@synthesize user;

+(instancetype)instance{
    if (!data) {
        data = [[self alloc] init];
        //创建文件夹
        if (![[NSFileManager defaultManager] fileExistsAtPath:AUDIOPATH]) {
            NSError *error;
            [[NSFileManager defaultManager] createDirectoryAtPath:AUDIOPATH withIntermediateDirectories:NO attributes:nil error:&error];
        }
        if (![[NSFileManager defaultManager] fileExistsAtPath:MAPPATH]) {
            NSError *error;
            [[NSFileManager defaultManager] createDirectoryAtPath:MAPPATH withIntermediateDirectories:NO attributes:nil error:&error];
        }
        if (![[NSFileManager defaultManager] fileExistsAtPath:ZIPCACHEPATH]) {
            NSError *error;
            [[NSFileManager defaultManager] createDirectoryAtPath:ZIPCACHEPATH withIntermediateDirectories:NO attributes:nil error:&error];
        }
        if (![[NSFileManager defaultManager] fileExistsAtPath:ZIPDOCUMENTPATH]) {
            NSError *error;
            [[NSFileManager defaultManager] createDirectoryAtPath:ZIPDOCUMENTPATH withIntermediateDirectories:NO attributes:nil error:&error];
        }
        if (![[NSFileManager defaultManager] fileExistsAtPath:UNZIPDOCUMENTPATH]) {
            NSError *error;
            [[NSFileManager defaultManager] createDirectoryAtPath:UNZIPDOCUMENTPATH withIntermediateDirectories:NO attributes:nil error:&error];
        }
    }
    return data;
}

-(id)init{
    if(self = [super init]){
        [self loadUserInfo];
        [self getLanguageValue:[self getLanguageType]];
    }
    return self;
}

-(BOOL)isFirstload{
    NSNumber *firstload = [[NSUserDefaults standardUserDefaults] objectForKey:@"FIRSTLOAD"];
    if (firstload) {
        return [firstload boolValue];
    }
    [[NSUserDefaults standardUserDefaults] setObject:@NO forKey:@"FIRSTLOAD"];
    return YES;
}

-(void)loadUserInfo{
    NSDictionary *info = [[NSUserDefaults standardUserDefaults] objectForKey:@"USERINFO"];
    if (info) {
        if (!user) {
            user = [[UserDTO alloc] init];
        }
        user.userPhone = info[@"phone"];
        user.code = info[@"code"];
        user.recommendNum = info[@"recommend"]?info[@"recommend"]:@"0";
        UIImage *image = [UIImage imageWithContentsOfFile:ICONPATH];
        user.userIcon = image;
        UIImage *image1 = [UIImage imageWithContentsOfFile:WORKICONPATH];
        user.GuideIcon = image1;
    }
}

-(void)setUserInfo:(UserDTO*)dto{
    user = dto;
    if (dto) {
        NSDictionary *dic = @{@"phone": user.userPhone,@"code":user.code,@"recommend":user.recommendNum};
        [[NSUserDefaults standardUserDefaults] setObject:dic forKey:@"USERINFO"];
    }else{
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"USERINFO"];
    }
}

-(void)setLanguangeType:(language_type)type{
    [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:type] forKey:@"LANGUAGE"];
    [self getLanguageValue:type];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"RESETLANGUAGE" object:[NSNumber numberWithInt:type]];
}

-(language_type)getLanguageType{
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"LANGUAGE"]) {
        return [[[NSUserDefaults standardUserDefaults] objectForKey:@"LANGUAGE"] integerValue];
    }
    
    //set default language chinese
    return zh_cn;
}

-(void)getLanguageValue:(language_type)type{
    switch (type) {
        case zh_cn:         //中文
            language = [[NSMutableDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"zh-cn" ofType:@"plist"]];
            break;
        case en_us:         //英文
            language = [[NSMutableDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"en-us" ofType:@"plist"]];
        default:
            language = [[NSMutableDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"en-us" ofType:@"plist"]];
            break;
    }
}

-(NSString*)getStringForKey:(NSString*)key{
    return language[key];
}

@end
